package com.kedll.library.util;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.kedll.library.listener.SimpleImageLoadingListener;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

/**
 * Created by QiQi on 2016/12/15.
 */

public class ImageLoadUtil {
    RequestOptions options;
    private Drawable onShowError;
    private Drawable onShowLoading;
    private SimpleImageLoadingListener listener;

    private ImageLoadUtil() {
    }

    public static ImageLoadUtil getInstance() {
        return ImageLoadUtilHolder.loadUtil;
    }

    /**
     * 设置当请求出错时显示的图片
     *
     * @param onShowError
     * @return
     */
    public ImageLoadUtil setOnShowError(int onShowError) {
        this.onShowError = Resources.getSystem().getDrawable(onShowError);
        return this;
    }

    public ImageLoadUtil setOnShowError(Drawable onShowError) {
        this.onShowError = onShowError;
        return this;
    }

    /**
     * 设置图片在加载中显示的图片
     *
     * @param onShowLoading
     * @return
     */
    public ImageLoadUtil setplaceholder(int onShowLoading) {
        this.onShowLoading = Resources.getSystem().getDrawable(onShowLoading);
        return this;
    }

    public ImageLoadUtil setplaceholder(Drawable onShowLoading) {
        this.onShowLoading = onShowLoading;
        return this;
    }

    /**
     * 设置图片加载回调
     *
     * @param imageLoadingListener
     * @return
     */
    public ImageLoadUtil setImageLoadingListener(SimpleImageLoadingListener imageLoadingListener) {
        this.listener = imageLoadingListener;
        return this;
    }

    public void ImageLoad(Activity context, String url, ImageView imageView) {
        load(Glide.with(context), url, imageView);
    }

    private void load(RequestManager request, final String url, final ImageView imageView) {
        if (options == null) {
            options = RequestOptions
                    .placeholderOf(onShowLoading)
                    .error(onShowError)
                    .centerCrop();
        }

        request.load(url)
                .apply(options)
                .thumbnail(0.5f)
                .transition(withCrossFade())
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        if (listener != null) {
                            listener.onLoadingFailed(url, imageView);
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (listener != null) {
                            listener.onLoadingComplete(url, imageView, resource);
                        }
                        return false;
                    }
                })
                .into(imageView);
    }

    public void ImageLoad(android.support.v4.app.Fragment context, String url, ImageView imageView) {
        load(Glide.with(context), url, imageView);
    }

    public void ImageLoad(Fragment context, String url, ImageView imageView) {
        load(Glide.with(context), url, imageView);
    }

    public void ImageLoad(Context context, String url, ImageView imageView) {
        load(Glide.with(context), url, imageView);
    }

    /**
     * 清楚缓存
     *
     * @param ctx
     */
    public void clearCache(Context ctx) {
        Glide.get(ctx).clearDiskCache();
        Glide.get(ctx).clearMemory();
    }

    private final static class ImageLoadUtilHolder {
        static ImageLoadUtil loadUtil = new ImageLoadUtil();
    }

}
